//
//  CXMBShow.swift
//  Asman
//
//  Created by 李旭峰 on 5/20/19.
//  Copyright © 2019 李旭峰. All rights reserved.
//

import UIKit
import Foundation
import MBProgressHUD

class CXMBShow: NSObject {
    
    /// 创建MB指示器
    ///
    /// - Parameters:
    ///   - message: 信息
    ///   - view: 所在的view
    @objc static func openIndicator(message:String?, view:UIView?) {
        
        MBProgressHUD.showMessage(message: message, toView: view != nil ? view : CXMBShow.currentView())
    }
    
    ///关闭指示器
    @objc static func colseIndicator(view:UIView? = nil) {
        MBProgressHUD.hideHUDForView(view: view != nil ? view : CXMBShow.currentView())
    }
    
    
    /// 提示信息
    ///
    /// - Parameter message: 提示信息的文本
    @objc static func showMessage(_ message:String) {
        DispatchQueue.main.async {
            if message == "Invalid" { return }
            if message.count >= 100 {
                UIApplication.shared.keyWindow?.displayMessage(message: "系统异常，请重试。")
            }else if message.count >= 1{
                UIApplication.shared.keyWindow?.displayMessage(message: message)
            }
        }
    }
    
    class func currentController() -> UIViewController? {
        if UIApplication.shared.keyWindow == nil {
            return nil
        }
//        let window = UIApplication.shared.keyWindow!
//        if let tabVc = window.rootViewController as? BaseTabBarViewController,let selectNavi =
//            tabVc.selectedViewController as? NavigationController {
//            if let vc = selectNavi.rui_viewControllers.last {
//                return vc
//            }
//        }
        return nil
    }
    
    
    class func currentView() -> UIView {
        let window = UIApplication.shared.keyWindow!
//        if let tabVc = window.rootViewController as? BaseTabBarViewController,let selectNavi = tabVc.selectedViewController as? NavigationController {
//            if let vc = selectNavi.rui_viewControllers.last {
//                if selectNavi.rui_viewControllers.count == 1 {
//                    return window
//                }
//                return vc.view
//            }
//        }
        return window
    }
}
